package 临时工具.小工具;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

//select id,gxqdx02,	gxqdy02,gxzdx02,	gxzdy02,sspq from env_pipeline_segment
public class LatLngRectangleBuffer {


    // 经纬度坐标类
    static class LatLng {
        double lon;
        double lat;
        LatLng(double lon, double lat) {
            this.lon = lon;
            this.lat = lat;
        }
        @Override
        public String toString() {
            return "[" + lon + ", " + lat + "]";
        }
    }

    // 地球半径（单位：米）
    private static final double EARTH_RADIUS = 6378137;

    public static void main(String[] args) {
        double bufferInMeters = 50;
        //读取文件
        String filePath = "C:\\Users\\瑾瑜\\Desktop\\sql\\formal\\无标题.json";
        String content = FileUtil.readUtf8String(filePath);
        List<JSONObject> jsonObjects = JSONArray.parseArray(content, JSONObject.class);
        StringBuilder stringBuilder = new StringBuilder();
        for (JSONObject jsonObject : jsonObjects) {
            // 原始经纬度点
            LatLng p1 = new LatLng(jsonObject.getDouble("gxqdx02"),jsonObject.getDouble("gxqdy02"));
            LatLng p2 = new LatLng(jsonObject.getDouble("gxzdx02"),jsonObject.getDouble("gxzdy02"));
            LatLng[] rectangle = computeBufferRectangle(p1, p2, bufferInMeters);
            String format = StrUtil.format("INSERT INTO `t_cruise_pipe_range_ex` " +
                            "(`id`, `cruise_object_id`, `transp_mgmt_office_id`, `lng_1`, `lat_1`, `lng_2`, `lat_2`, `lng_3`, `lat_3`, `lng_4`, `lat_4`, `create_time`, `update_time`) VALUES " +
                            "({}, {}, '{}', {}, {}, {}, {}, {}, {}, {}, {}, NULL, NULL);\n",
                    jsonObject.getInteger("id"),
                    jsonObject.getInteger("id"),
                    jsonObject.getString("sspq"),
                    rectangle[0].lat,
                    rectangle[0].lon,
                    rectangle[1].lat,
                    rectangle[1].lon,
                    rectangle[2].lat,
                    rectangle[2].lon,
                    rectangle[3].lat,
                    rectangle[3].lon
            );
            if(format.contains("NaN")){
                continue;
            }
            stringBuilder.append(format);

            // 打印结果
            System.out.println( jsonObject.getInteger("id"));

        //把结果写入文件

        }
        //清空"C:\\Users\\瑾瑜\\Desktop\\sql\\formal\\1.sql"
        FileUtil.writeUtf8String("", "C:\\Users\\瑾瑜\\Desktop\\sql\\formal\\1.sql");
        FileUtil.appendUtf8String(stringBuilder.toString(), "C:\\Users\\瑾瑜\\Desktop\\sql\\formal\\1.sql");










    }
    //计算矩形


    // 主计算函数
    public static LatLng[] computeBufferRectangle(LatLng p1, LatLng p2, double distanceMeters) {
        // 将经纬度转为米制坐标（简化）
        double lat1 = Math.toRadians(p1.lat);
        double lon1 = Math.toRadians(p1.lon);
        double lat2 = Math.toRadians(p2.lat);
        double lon2 = Math.toRadians(p2.lon);

        // 经度和纬度差值转为米（近似）
        double dx = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2) * EARTH_RADIUS;
        double dy = (lat2 - lat1) * EARTH_RADIUS;

        // 方向单位向量
        double length = Math.sqrt(dx * dx + dy * dy);
        double ux = dx / length;
        double uy = dy / length;

        // 垂直方向（顺时针旋转90度）
        double nx = -uy;
        double ny = ux;

        // 偏移值（米）
        double offsetX = nx * distanceMeters;
        double offsetY = ny * distanceMeters;

        // 把偏移转换为经纬度偏移
        double offsetLat = offsetY / EARTH_RADIUS * (180 / Math.PI);
        double offsetLon = offsetX / (EARTH_RADIUS * Math.cos((lat1 + lat2) / 2)) * (180 / Math.PI);

        // 计算四个点（顺时针）
        LatLng r1 = new LatLng(p1.lon + offsetLon, p1.lat + offsetLat);
        LatLng r2 = new LatLng(p2.lon + offsetLon, p2.lat + offsetLat);
        LatLng r3 = new LatLng(p2.lon - offsetLon, p2.lat - offsetLat);
        LatLng r4 = new LatLng(p1.lon - offsetLon, p1.lat - offsetLat);

        return new LatLng[]{r1, r2, r3, r4};
    }
}
